package com.campus.dao.impl;

import com.campus.dao.CourseDao;
import com.campus.model.Course;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author HLQ
 * @date 2024/11/18 21:40
 */
@Slf4j
@Repository
public class CourseDaoImpl implements CourseDao {
    @Autowired
    JdbcTemplate jdbcTemplate;

    private final RowMapper<Course> courseRowMapper = new RowMapper<Course>() {
        @Override
        public Course mapRow(ResultSet rs, int rowNum) throws SQLException {
            Course course = new Course()
                    .setId(rs.getInt("id"))
                    .setCourseName(rs.getString("course_name"))
                    .setCreateTime(rs.getTime("create_time"))
                    .setStatus(rs.getInt("status"))
                    .setActivityId(rs.getInt("activity_id"))
                    .setTeacherId(rs.getInt("teacher_id"))
                    .setClassId(rs.getInt("class_id"));
            return course;
        }
    };
    @Override
    public int addCourse(Course course) {
        String sql = "INSERT INTO t_course " +
                "(course_name, create_time, status, activity_id, teacher_id, class_id) " +
                "VALUES (?,?,?,?,?,?)";
        Object[] args = new Object[]{
                course.getCourseName(),
                course.getCreateTime(),
                course.getStatus(),
                course.getActivityId(),
                course.getTeacherId(),
                course.getClassId()
        };
        return jdbcTemplate.update(sql,args );
    }

    @Override
    public int deleteCourseById(int id) {
        String sql = "DELETE FROM t_course WHERE id = ?";
        return jdbcTemplate.update(sql, id);
    }

    @Override
    public int updateCourseById(Course course) {
        String sql = "UPDATE t_course SET " +
                "course_name = ?, create_time = ?, status = ?, activity_id = ?, " +
                "teacher_id = ?, class_id = ? WHERE id = ?";
        Object[] args = new Object[]{
                course.getCourseName(),
                course.getCreateTime(),
                course.getStatus(),
                course.getActivityId(),
                course.getTeacherId(),
                course.getClassId(),
                course.getId()
        };
        return jdbcTemplate.update(sql, args);
    }

    @Override
    public Course queryCourseById(int id) {
        String sql = "SELECT * FROM t_course WHERE id = ?";
        return jdbcTemplate.queryForObject(sql, new Object[]{id}, courseRowMapper);

    }

    @Override
    public Course queryCourseByName(String name) {
        String sql = "SELECT * FROM t_course WHERE course_name = ?";
        return jdbcTemplate.queryForObject(sql, new Object[]{name}, courseRowMapper);
    }
}
